package com.ellecity06.common.engine

import android.R
import android.content.Context
import android.graphics.Bitmap
import android.graphics.drawable.Drawable
import android.net.Uri
import android.widget.ImageView
import androidx.annotation.Nullable
import androidx.fragment.app.Fragment
import com.bumptech.glide.Glide
import com.bumptech.glide.request.target.CustomTarget
import com.bumptech.glide.request.transition.Transition
import com.luck.picture.lib.engine.CropFileEngine
import com.luck.picture.lib.style.SelectMainStyle
import com.luck.picture.lib.style.TitleBarStyle
import com.luck.picture.lib.utils.StyleUtils
import com.yalantis.ucrop.UCrop
import com.yalantis.ucrop.UCropImageEngine


/**
 * @author ellecity06
 * @time 2024/11/7 18:15
 * @des
 */
class CustomCropFileEngine : CropFileEngine {
    override fun onStartCrop(
        fragment: Fragment?,
        srcUri: Uri?,
        destinationUri: Uri?,
        dataSource: ArrayList<String>?,
        requestCode: Int
    ) {
                    val options: UCrop.Options = buildOptions()
        val uCrop = UCrop.of(srcUri!!, destinationUri!!, dataSource)
                    uCrop.withOptions(options)
        uCrop.setImageEngine(object : UCropImageEngine {
            override fun loadImage(
                context: Context,
                url: String,
                imageView: ImageView
            ) {

                Glide.with(context).load(url).override(180, 180).into(imageView)
            }

            override fun loadImage(
                context: Context?,
                url: Uri?,
                maxWidth: Int,
                maxHeight: Int,
                call: UCropImageEngine.OnCallbackListener<Bitmap?>?
            ) {
                Glide.with(context!!).asBitmap().load(url).override(maxWidth, maxHeight)
                    .into(object : CustomTarget<Bitmap?>() {


                        override fun onLoadCleared(@Nullable placeholder: Drawable?) {
                            call?.onCall(null)
                        }

                        override fun onResourceReady(
                            resource: Bitmap,
                            transition: Transition<in Bitmap?>?
                        ) {
                            call?.onCall(resource)
                        }
                    })
            }
        })
        uCrop.start(fragment!!.requireActivity(), fragment!!, requestCode)
    }
    private fun buildOptions(): UCrop.Options {
        val options = UCrop.Options()
        options.setHideBottomControls(true)
        options.setFreeStyleCropEnabled(true)
        options.setShowCropFrame(false)
        options.setShowCropGrid(true)
        // 圆形图片
        options.setCircleDimmedLayer(false)
        options.withAspectRatio(1F, 1F)
//        options.setCropOutputPathDir(getSandboxPath())
        options.isCropDragSmoothToCenter(false)
//        options.setSkipCropMimeType(getNotSupportCrop())
//        options.isForbidCropGifWebp(cb_not_gif.isChecked())
        options.isForbidSkipMultipleCrop(true)
        options.setMaxScaleMultiplier(100f)
        return options
    }
    companion object {
        private var instance: CustomCropFileEngine? = null
        fun createCropEngine(): CustomCropFileEngine? {
            if (null == instance) {
                synchronized(CustomCropFileEngine::class.java) {
                    if (null == instance) {
                        instance = CustomCropFileEngine()
                    }
                }
            }
            return instance
        }
    }
}